package mess;

import java.time.temporal.Temporal;
import java.util.List;

public class LeeCode_02_3 {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));
        System.out.println(addTwoNumbers(l1, l2));
        System.out.println("-----------");
        ListNode l3 = new ListNode(0);
        ListNode l4 = new ListNode(0);
        System.out.println(addTwoNumbers(l3, l4));
       ListNode l5 = new ListNode(2, new ListNode(4, new ListNode(9)));
        ListNode l6 = new ListNode(5, new ListNode(6, new ListNode(4, new ListNode(9))));
        System.out.println(addTwoNumbers(l5, l6));
    }

    private static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode start0 = new ListNode(0);
        ListNode start1 = start0;
        int num1 = 0;
        int num2 = 0;
        int num3 = 0;
        int temp = 1;
        while (l1 != null){
            num1 += l1.val * temp;
            temp *= 10;
            l1 = l1.next;
        }
        System.out.println(num1);
        temp = 1;
        while (l2 != null){
            num2 += l2.val * temp;
            temp *= 10;
            l2 = l2.next;
        }
        System.out.println(num2);
        num3 = num1 + num2;
        if (num3 == 0)
            return start0;
        while (num3 != 0){
            int ge = num3 % 10;
            num3 /= 10;
            start1.next = new ListNode(ge);
            start1 = start1 .next;
        }
        return start0.next;
    }

    private static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return val + "->" + this.next;
        }
    }
}
